package com.bw.zg2.view.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alipay.sdk.app.PayTask
import com.blankj.utilcode.util.ToastUtils
import com.bw.zg2.Const.Companion.PATH_GOODSDETAIL
import com.bw.zg2.R
import com.bw.zg2.base.BaseActivity
import com.bw.zg2.databinding.ActivityGoodsDetailBinding
import com.bw.zg2.utils.GlideUtil
import com.bw.zg2.utils.SpUtil
import com.bw.zg2.viewmodel.GoodsDetailViewModel


@Route(path = PATH_GOODSDETAIL)
class GoodsDetailActivity :
    BaseActivity<GoodsDetailViewModel, ActivityGoodsDetailBinding>(R.layout.activity_goods_detail) {


    //Autowired 注入的字段必须为 public 或者 internal，否则无法注入。
    //在 kotlin 中，会默认生成 setter 和 getter，所以必须使用 @JvmField 标注对应的字段，
    // 或者使用 lateinit var 定义对应字段。

    //@JvmField
    //@Autowired
    var goodsId: Int = 0

    lateinit var desc: String
    var price: Int = 0

    override fun initData() {
        viewModel.liveDataGoodsDetail.observe(this) {
            //设置详情banner
            dataBinding.banner.setData(it.data.bannerList, null)
            dataBinding.banner.setmAutoPlayAble(true)
            dataBinding.banner.setmAutoPalyTime(1500)
            dataBinding.banner.setmAdapter { banner, view, position ->
                GlideUtil.loadCornerImg(
                    this@GoodsDetailActivity,
                    it.data.bannerList[position],
                    view as ImageView
                )
            }

            desc = it.data.goods_desc
            price = it.data.goods_default_price
            dataBinding.tvPrice.text = "¥${it.data.goods_default_price}"
            dataBinding.tvDesc.text = it.data.goods_desc
        }
        //接收添加购物车返回数据
        viewModel.liveDataGoodsCart.observe(this) {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
//
            }
        }

        //接收删除购物车返回数据
        viewModel.liveDataDeleteGoodsCart.observe(this) {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
//
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
//        ARouter.getInstance().inject(this)
        goodsId = intent.getIntExtra("goodsId", 0)
        viewModel.getDetail(goodsId)

        dataBinding.ivBack.setOnClickListener { finish() }

        //加入购物车
        dataBinding.tvCart.setOnClickListener {
            val map = HashMap<String, Int>()
            map["count"] = 1
            map["goods_id"] = goodsId
            viewModel.addCart(map)
        }

        //加入购物车
        dataBinding.tvDelerte.setOnClickListener {
            val map = HashMap<String, String>()
            map["partnerId"] = "23022113430907CF0113"
            map["requestNo"] = "202409260941213420"
            viewModel.gateway(map)
        }

        //立即购买
        dataBinding.tvBuy.setOnClickListener {
            //调用支付宝sdk,模拟支付流程
            toPay()
        }
    }

    fun toPay() {

        val orderInfo: String =
            "app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2&timestamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D" // 订单信息
        val payRunnable = Runnable {
            val alipay = PayTask(this@GoodsDetailActivity)
            val result = alipay.payV2(orderInfo, true)
            if (result != null) {
                //支付成功,当前在子线程
                //变更余额
                val pn = SpUtil.getInt("money") - price
                //保存最新余额
                SpUtil.putInt("money", pn)
                val intent = Intent(this@GoodsDetailActivity, PayResultActivity::class.java)
                intent.putExtra("desc", desc)
                intent.putExtra("price", price)
                startActivity(intent)
            }
        }
        // 必须异步调用
        val payThread = Thread(payRunnable)
        payThread.start()
    }

    fun share(){
        // 分享
        val shareIntent = Intent()
        //发送文字到qq
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.type = "text/plain"
        shareIntent.putExtra(Intent.EXTRA_TEXT, "新闻标题:" + ",新闻内容：")
        startActivity(Intent.createChooser(shareIntent, "分享新闻到"))
    }

    fun shareImg(uriToImage:Uri){
        val shareIntent: Intent = Intent()
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage)
        shareIntent.type = "image/jpeg"
        startActivity(Intent.createChooser(shareIntent,"分享到" ))
    }
}